package app.server.project.main.service.service.upay.tron;

import app.server.project.main.service.db.dao.cfg.GlobalCfgDao;
import app.server.project.main.service.service.upay.tron.constants.APIKey;
import app.server.project.main.service.service.upay.tron.constants.Net;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.tron.trident.core.ApiWrapper;

import static app.server.project.main.service.service.upay.tron.constants.Address.Main_USDT;
import static app.server.project.main.service.service.upay.tron.constants.Address.Nile_USDT;
import static app.server.project.main.service.service.upay.tron.constants.TronConstants.*;

@Service
@RequiredArgsConstructor
public class CfgService {

    private final GlobalCfgDao globalCfgDao;
    private ApiWrapper blockClient;
    private ApiWrapper activeWalletClient;


    @EventListener(ApplicationReadyEvent.class)
    public void init() {
        this.activeWalletClient = this.getActiveWalletClient();
        this.blockClient = this.getBlockClient();
    }

    /**
     * 获取激活账号的TronClient
     *
     * @return
     */
    public ApiWrapper getActiveWalletClient() {
        if (this.activeWalletClient != null) {
            return this.activeWalletClient;
        }
        String activeUrl = Tron_Active_URL;
        if (this.globalCfgDao.has(Global_Tron_Active_URL)) {
            activeUrl = this.globalCfgDao.getOneByKey(Global_Tron_Active_URL).getVal();
        }
        var activePrivateKey = Tron_Active_Private_Key;
        if (this.globalCfgDao.has(Global_Tron_Active_Private_Key)) {
            activePrivateKey = this.globalCfgDao.getOneByKey(Global_Tron_Active_Private_Key).getVal();
        }
        boolean debug = Net.Chain_Net_Value;
        if (this.globalCfgDao.has(Net.Chain_Net_Key)) {
            debug = this.globalCfgDao.getOneByKey(Net.Chain_Net_Key).getBool();
        }

        if (debug) {
            activeWalletClient = ApiWrapper.ofNile(activePrivateKey);
        } else {
            activeWalletClient = ApiWrapper.ofMainnet(activePrivateKey, APIKey.Tron_Active_Account_API_KEY);
        }
        return activeWalletClient;
    }

    public ApiWrapper getClient(String privateKey) {
        ApiWrapper client = null;
        boolean debug = Net.Chain_Net_Value;
        if (this.globalCfgDao.has(Net.Chain_Net_Key)) {
            debug = this.globalCfgDao.getOneByKey(Net.Chain_Net_Key).getBool();
        }
        if (debug) {
            client = ApiWrapper.ofNile(privateKey);
        } else {
            client = ApiWrapper.ofMainnet(privateKey, APIKey.Tron_Active_Account_API_KEY);
        }
        return client;
    }

    /**
     * 获取区块信息客户端
     *
     * @return
     */
    public ApiWrapper getBlockClient() {
        if (this.blockClient != null) {
            return this.blockClient;
        }
        var activePrivateKey = Tron_Active_Private_Key;
        if (this.globalCfgDao.has(Global_Tron_Active_Private_Key)) {
            activePrivateKey = this.globalCfgDao.getOneByKey(Global_Tron_Active_Private_Key).getVal();
        }
        boolean debug = Net.Chain_Net_Value;
        if (this.globalCfgDao.has(Net.Chain_Net_Key)) {
            debug = this.globalCfgDao.getOneByKey(Net.Chain_Net_Key).getBool();
        }

        if (debug) {
            this.blockClient = ApiWrapper.ofNile(activePrivateKey);
        } else {
            this.blockClient = ApiWrapper.ofMainnet(activePrivateKey, APIKey.Now_API_KEY);
        }
        return this.blockClient;
    }

    /**
     * 获取USDT合约地址
     *
     * @return
     */
    public String getUSDTContractAddress() {
        boolean debug = Net.Chain_Net_Value;
        if (this.globalCfgDao.has(Net.Chain_Net_Key)) {
            debug = this.globalCfgDao.getOneByKey(Net.Chain_Net_Key).getBool();
        }
        return debug ? Nile_USDT : Main_USDT;
    }


    public String getBaseUrl() {
        boolean debug = Net.Chain_Net_Value;
        if (this.globalCfgDao.has(Net.Chain_Net_Key)) {
            debug = this.globalCfgDao.getOneByKey(Net.Chain_Net_Key).getBool();
        }
        return debug ? Net.Test_Base_Url : Net.Main_Base_Url;
    }

}
